package cn.edu.hqu.javaee.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;


import cn.edu.hqu.javaee.domain.Personal;
import cn.edu.hqu.javaee.repository.PersonalRepository;



@Controller
@RequestMapping("/checkout")
@SessionAttributes("personal")
public class PersonalController {
	private PersonalRepository personalRepo;

	public PersonalController(PersonalRepository personalRepo) {
		this.personalRepo = personalRepo;
	}
	
	@GetMapping("/personal")
	public String personal() {
		return "personal";
	}

	@PostMapping
	public String processPersonal(@Valid Personal personal, Errors errors, SessionStatus sessionStatus)  {
		if (errors.hasErrors()) {
			return "personal";
		}
		personalRepo.save(personal);
		sessionStatus.setComplete();
	    System.out.println("personal"+personal);
		return "last";
	}
}
